//
//  FKNetworkingManager.h
//  FKNetWorking
//
//  Created by 云眼科技 on 16/7/18.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFNetworking.h"
#import "FKNetworkingDownloadModel.h"

NS_ASSUME_NONNULL_BEGIN

NS_CLASS_AVAILABLE_IOS(7_0) @interface FKNetworkingManager : NSObject

@property (nonatomic, copy, readonly) NSString *downloadDirectory;

@property (nonatomic, strong, readonly) NSMutableArray <__kindof FKNetworkingDownloadModel *> *downloadingModels;

@property (nonatomic, strong, readonly) NSMutableArray <__kindof FKNetworkingDownloadModel *> *waitingModels;

@property (nonatomic, assign) NSInteger maxDownloadCount;

@property (nonatomic, assign) BOOL resumeTaskFIFO;

@property (nonatomic, assign, getter=isBatchDownload) BOOL batchDownload;

/*
 *  key-values dictionary of the downloadModels, format as '<NSString *key, FKDownloadModel *model>' to make constraints
 *  used to find a downloadModel from this container, 
 *  when the program will terminate, container will be clear
 */
@property (nonatomic, strong) NSMutableDictionary <NSString *, __kindof FKNetworkingDownloadModel *> *downloadModelsDict;


+(FKNetworkingManager *)shareManager;

#pragma mark - get method
-(void)fk_getWithURLString:(NSString *)URLString 
                 parameter:(NSDictionary *)parameter
              successBlock:(nullable void (^)(id  _Nullable responseObject))successBlock 
              failureBlock:(nullable void (^)(NSError *error))failureBlock;

#pragma mark - post method
-(void)fk_postToURLString:(NSString *)URLString 
                    parameter:(NSDictionary *)parameter
                 successBlock:(nullable void (^)(id  _Nullable responseObject))successBlock 
                 failureBlock:(nullable void (^)(NSError *error))failureBlock;

#pragma mark - upload methods
//-(void)fk_upload

#pragma mark - download methods

/**
 ===============> Description of download start <=============
 = when you wanna download a file from serverce, you can use next methods as you need. 
 = there have some methods such as start, resume, cancel and so on.
 = We add the 'cancel' method to pause a task instead of suspend, because we need to resume the task when the App restart.
 = all methods will use a download model who is subclass of the 'FKNetworkingDownloadModel' for convenience. 
 = and every model use a URL string as the primary key to mark a download mission or task.
 ===============> Description of download end. <=============
 */


/**
 *  this method used to start a download mission with a download model, notice that the download model can't be nil, or the download mission will not execute.
 *
 *  @param downloadModel     download model
 *  @param progress          progress of the download, track to refresh UI and ...
 *  @param completionHandler you can doing something after download mission completed, such as refresh your UI and move the file and so on.
 */
-(void)fk_startDownloadWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel 
                                progress:(void (^)(FKNetworkingDownloadModel *downloadModel))progress
                       completionHandler:(void (^)(FKNetworkingDownloadModel *downloadModel, NSError * _Nullable error))completionHandler;


/**
 *  resume a download task with a download model, it will use the download model's 'resumeData'
 *
 *  @param downloadModel download model 
 */
-(void)fk_resumeDownloadWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel;                          

/**
 *  suspend or cancel a download task
 *
 *  @param downloadModel download model
 */
-(void)fk_cancelDownloadTaskWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel;

/**
 *  check the resourece has been downloaded or not from the download model resourceURL.
 *
 *  @param downloadModel download model
 *
 *  @return YES or NO.
 */
-(BOOL)fk_hasDownloadedFileWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel; 

/**
 *  delete local file if exist.
 *
 *  @param downloadModel download model.
 */
-(void)fk_deleteDownloadedFileWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel;

/**
 *  delete all downloaded files.
 */
-(void)fk_deleteAllDownloadedFiles;

/**
 *  get a download model, which is downloading with a URLString. if there is not exist a model, will return nil.
 *
 *  @param URLString URLString.
 *
 *  @return download model
 */
-(nullable FKNetworkingDownloadModel *)fk_getDownloadingModelWithURLString:(NSString *)URLString;

/**
 *  get download progress information. such as download speed, progress and so on.
 *
 *  @param downloadModel download model.
 *
 *  @return progress model.
 */
-(nullable FKNetworkingProgressModel *)fk_getDownloadProgressModelWithDownloadModel:(FKNetworkingDownloadModel *)downloadModel;


@end

NS_ASSUME_NONNULL_END
